import sys

from LMG.Utility.constants import *

################################################################

class TorrentStatus:
    """
    Keep track of the status of a torrent
    """
    def __init__(self, torrent):
        self.torrent = torrent
        
        # set status
        if utility.config.Read('defaultstatus', "boolean"):
            self.value = STATUS_QUEUE
        else:
            self.value = STATUS_STOP            
        self.completed = False
        self.forced = False
        self.dontupdate = True # Don't update until the list item is created

        self.autostarted = False

    def getStatusText(self):
        value = self.value
        
        if self.isActive():
            if value == STATUS_PAUSE:
                status = _('paused')
            elif value == STATUS_SUPERSEED:
                status = _('super-seeding')
            elif self.torrent.connection.engine is not None:
                status = self.torrent.connection.engine.btstatus
            else:
                status = _('stopping')
        elif value == STATUS_FINISHED:
            status = _('completed')
        elif value == STATUS_STOP:
            status = _('stopped')
        elif value == STATUS_QUEUE:
            status = _('queued')
        else:
            # Most likely just not quite started yet
            status = _('waiting')
        
        return status
        
    def isActive(self, working = True, checking = True, pause = True):
        """
        Is the torrent active?
        """
        engine = self.torrent.connection.engine
        if engine is None:
            return False
        else:
            if not pause and self.value == STATUS_PAUSE:
                return False
            if working and engine.working:
                return True
            elif checking and (engine.checking or engine.waiting):
                return True
                
    def isCheckingOrAllocating(self):
        """
        See if the torrent is checking existing data or allocating
        """
        # If the torrent is in its initialization stage, the progress value
        # we get from ABCEngine won't reflect the download progress
        # 
        # Note: "moving data" is a third initialization status that is listed
        #       in the BitTornado source
        ######################################################################
        if self.torrent.connection.engine is not None:
            status = self.getStatusText()
            statuslist = [ _('waiting'), 
                           _('checking data'), 
                           _('allocating space'), 
                           _('moving data') ]
            if (status in statuslist):
                return True
        return False
        
    def isDoneUploading(self):
        """
        See if a torrent is finished uploading
        """     
        finished = False     

        # If the file isn't finished, or it's set to unlimited upload
        if self.torrent.files.progress != 100.0:
            pass

        # Forced seeding
        elif self.torrent.status.forced:
            pass
        
        # If upload ratio is not oo check if ratio limit reached
        elif self.torrent.connection.getSeedOption('uploadratio') != "0" \
             and self.torrent.getColumnValue(COL_RATIO) >= float(self.torrent.connection.getSeedOption('uploadratio')):
            finished = True
            
        # If upload time is not oo check if time reached
        else:
            uploadtimes = self.torrent.connection.getTargetSeedingTime()
            if uploadtimes < 1800: # Unlimited upload!
                pass
            elif self.torrent.connection.seedingtime >= uploadtimes:
                finished = True
        
        # Also mark as completed in case it wasn't for some reason
        if finished:
            self.value = STATUS_FINISHED
            self.completed = True
            
        elif self.value == STATUS_FINISHED:
            # Was finished before, but isn't now
            self.value = STATUS_QUEUE
            
        self.torrent.updateColumns([COL_BTSTATUS])
        
        return finished
        
    def updateStatus(self, value, update = True):
        """
        Update the status of a torrent
        """
        if value != self.value:
            self.value = value
            if update:
                self.torrent.torrentconfig.writeStatus()
        
